﻿using Practice.Domain.Shared;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice.PH4_2104A.Shopping.SevenLayerFramework.Domin
{
    //订单的基本信息
    /// <summary>
    /// 订单主表
    /// </summary>
    [Table("tb_order_master")]
    public class OrderMasterModel : AuditField
    {
        public Guid Id { get; set; }

        //和会员相关的
        /// <summary>
        /// 会员ID
        /// </summary>
        public Guid MemberId { get; set; } //会员ID
        public string RecivedName { get; set; } //收件人姓名
        public string RecivedPhone { get; set; } //收件人电话
        public string RecivedAddress { get; set; }   //收件人地址

        //订单基本信息
        public string OrderNo { get; set; } //订单编号  ---  雪花ID
        public DateTime OrderTime { get; set; } = System.DateTime.Now; //下单时间
        public decimal OrderAmount { get; set; } //订单总金额
        public decimal RateAmount { get; set; } //折扣金额
        public OrderStateEnum OrderState { get; set; } = OrderStateEnum.WaittingPay; //订单状态 ---- 枚举
        public string? Remark { get; set; } //订单描述。备注

        //付款信息
        public DateTime? PayTime { get; set; } //付款时间
        public decimal? PayAmount { get; set; } //付款金额
        public OrderPayTypeEnum? PayType { get; set; } //付款方式 ----- 枚举

        private string orderTimeLabel;
        [NotMapped]
        public string OrderTimeLabel
        {
            get
            {
                return this.OrderTime.ToString("yyyy/MM/dd hh:mm:ss");
            }
            set => orderTimeLabel = value;
        }

        private string orderStateLabel;
        [NotMapped]
        public string OrderStateLabel
        {
            get
            {
                orderStateLabel = this.OrderState.ToString();

                //获取Display特性的Name属性值
                var objs = this.OrderState.GetType().GetField(this.OrderState.ToString()).
                     GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), true);
                if (objs != null && objs.Length > 0)
                {
                    orderStateLabel = ((System.ComponentModel.DataAnnotations.DisplayAttribute)objs[0]).Name;
                }

                return orderStateLabel;
            }
            set => orderStateLabel = value;
        }


        //构造函数
        public OrderMasterModel()
        {
            this.OrderNo = SnowflakeExtend.GetWork().NextId().ToString();
        }
    }

    class SnowflakeExtend
    {
        /*
         * 单例有三个特征
         * 1、私有的构造函数（这个类，不能被实例化）
         * 2、私有的变量
         * 3、公共的，对外的静态方法
         * **/
        private SnowflakeExtend() { }
        private static Snowflake.Core.IdWorker _work;
        public static Snowflake.Core.IdWorker GetWork()
        {
            if (_work == null)
            {
                _work = new Snowflake.Core.IdWorker(1, 1);
            }
            return _work;
        }
    }
}
